/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.servlet;

import com.qdu.entity.Admin;
import com.qdu.entity.User;
import com.qdu.service.UserService;
import com.qdu.service.impl.UserServiceImpl;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sanshi
 */
@WebServlet(name = "UserServlet", urlPatterns = "/UserServlet")
public class UserServlet extends BaseServlet {

    private UserService us = new UserServiceImpl();

    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String useremail = request.getParameter("useremail");

        boolean b = us.regist(new User(null, username, password, useremail));
        if (b) {
            //注册成功，返回成功页面，重定向
             request.setAttribute("message", "注册成功！");
            request.getRequestDispatcher("/userLog.jsp").forward(request, response);

        } else {
            request.setAttribute("message", "用户名重复！请重新输入");
           request.getRequestDispatcher("/userLog.jsp").forward(request, response);
            //注册失败，返回注册页面，转发
             // String message="分类修改失败";
               // request.getSession().setAttribute("message", message);
          // response.sendRedirect(request.getContextPath() + "/pages/error.html");
           //response.sendRedirect(request.getContextPath() + "/userLog.jsp");
        }
    }

    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       HttpSession session = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //校验是管理员账号还是用户账号
        User user = us.login(new User(null, username, password));
        //管理员
        Admin admin = us.login(new Admin(username, password));
        if (user == null && admin == null) {
            //登录失败,返回登录页面，转发
             request.setAttribute("message", "请输入正确的用户名或密码");
            System.out.println("[" + username + "]用户登录失败！跳转去登录页面login.html");
            request.getRequestDispatcher("/userLog.jsp").forward(request, response);
        } else {
            if (user != null) {        
                //登录成功，返回成功界面 重定向
                
                user.setUpwd(password);
              session.setAttribute("user", user);
                System.out.println("[" + username + "]用户登录成功！跳转去登录成功页面login_success.html");
                response.sendRedirect(request.getContextPath() + "/#");
            }
            if(admin!=null){
                   //登录成功，返回成功界面 重定向
                  session.setAttribute("admin", admin);
                System.out.println("[" + username + "]狗管理登录成功！跳转去登录成功页面login_success.html");
                response.sendRedirect(request.getContextPath() + "/pages/cssthemes3/mbts_6_amovie/adminControl.jsp");
            }

        }
    }
}
